package exam.business.service;

import java.util.Date;
import java.util.List;

import exam.common.constant.ResultPort;
import exam.common.key.KeyQuestion;
import exam.common.util.NullCheckUtil;
import exam.model.dao.TAnswerDAO;
import exam.model.dao.TQuestionDAO;
import exam.model.entity.TAnswerEntity;
import exam.model.entity.TQuestionEntity;
import exam.system.business.AbstractService;
import exam.system.business.BusinessServiceInfo;
/**
 * <p>QuestionUpdateService</p>
 * 
 * @author Phong
 * @version (create: 21/5/2012)
 */
public class QuestionUpdateService extends AbstractService {
	// tQuestionDAO
	private TQuestionDAO tQuestionDAO = new TQuestionDAO();
	// tAnswerDAO
	private TAnswerDAO tAnswerDAO = new TAnswerDAO();
	@Override
	public String executeService(BusinessServiceInfo serviceInfo)
			throws Exception {
		// lay thong tin cau hoi
		TQuestionEntity tQuestionEntity = serviceInfo.getObject(KeyQuestion.T_QUESTION_ENTITY);
		// lay danh sach dap an
		List<TAnswerEntity> tAnswerEntityList = serviceInfo.getList(KeyQuestion.T_ANSWER_ENTITY_LIST);
		if(NullCheckUtil.isNotNull(tQuestionEntity)) {
			tQuestionEntity.setUpdateDate(new Date());
			// goi method cap nhat cua hoi
			int update = tQuestionDAO.updateQuestion(tQuestionEntity);
			if(update == 0) {
				return ResultPort.FAIL;
			}
			// cap nhat dap an
			TAnswerEntity tAnswerEntity = null;
			for(int i = 0; i < tAnswerEntityList.size(); i++) {
				tAnswerEntity = new TAnswerEntity();
				tAnswerEntity.setAnswerContent(tAnswerEntityList.get(i).getAnswerContent());
				tAnswerEntity.setAnswerCorrect(tAnswerEntityList.get(i).getAnswerCorrect());
				tAnswerEntity.setAnswerId(tAnswerEntityList.get(i).getAnswerId());
				tAnswerDAO.updateAnswer(tAnswerEntity);
			}
			return ResultPort.SUCCESS;
		}
		return ResultPort.FAIL;
	}

}
